import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:largefriends/pages/lf_base_controller.dart';
import 'package:largefriends/pages/lf_tab_contact/lf_contact_list_loading.dart';
import 'package:largefriends/res/lf_double_ext.dart';
import 'package:largefriends/utils/lf_scroll_behavior.dart';
import 'package:largefriends/widgets/common_failed_page.dart';

import '../../../res/lf_widget_ext.dart';
import '../../../widgets/user_card.dart';
import '../../lf_profile/lf_profile_logic.dart';
import '../../lf_tab_contact/contact_upgrade_banner.dart';
import 'lf_member_logic.dart';

class LfMemberPage extends StatefulWidget {
  const LfMemberPage({super.key});

  @override
  State<LfMemberPage> createState() => _LfMemberPageState();
}

class _LfMemberPageState extends State<LfMemberPage> {
  final logic = Get.put(LfMemberLogic());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: null,
      body: GetBuilder<LfMemberLogic>(
        builder: (logic) {
          if (logic.dataList.isEmpty) {
            if (logic.state == LoadingState.loading ||
                logic.state == LoadingState.init) {
              return const LfContactListLoading();
            } else if (logic.state == LoadingState.failed) {
              return CommonFailedPage(
                retry: () {
                  logic.getCustomParamHttpRequest(isRefresh: true);
                },
              );
            }
            return Container();
          }
          return Padding(
            padding: EdgeInsets.only(top: 10.h),
            child: ScrollConfiguration(
                behavior: LfScrollBehavior(),
                child: EasyRefresh(
                    controller: logic.refreshController,
                    onLoad: () async {
                      var result = await logic.getCustomParamHttpRequest(
                          isRefresh: false);
                      return result;
                    },
                    onRefresh: () async {
                      var result = await logic.getCustomParamHttpRequest(
                          isRefresh: true);
                      return result;
                    },
                    child: CustomScrollView(
                      slivers: [
                        logic.authService.currentAccount!.isGold()
                            ? empty.sliverBox
                            : getContactBanner(content: "UPGRADE TO SEE THE EXCLUSIVE VERIFIED MEMBERS LIST.").sliverBox,
                        SliverPadding(
                          padding: EdgeInsets.symmetric(horizontal: 10.w),
                          sliver: SliverList.separated(
                            itemBuilder: (context, index) {
                              var userEntity = logic.dataList[index];
                              return InkWell(
                                child: UserCardWidget(
                                  userEntity: userEntity,
                                  showNew: false,
                                  needBlur: logic.authService.currentAccount!
                                          .isGold() ==
                                      false,
                                ),
                                onTap: () {
                                  toProfilePage(
                                      uid: userEntity.userId!,
                                      userEntity: userEntity);
                                },
                              );
                            },
                            separatorBuilder: (context, index) {
                              return 15.h.sbh;
                            },
                            itemCount: logic.dataList.length,
                          ),
                        )
                      ],
                    ))),
          );
        },
      ),
    );
  }
}
